//
//  BankSelectView.m
//  Test
//
//  Created by figs on 15/11/21.
//  Copyright © 2015年 luoqj. All rights reserved.
//

#import "BankSelectView.h"
@interface BankSelectView()<UIPickerViewDelegate, UIPickerViewDataSource>{
    UIPickerView *bankPicker;
    UIView *contentView;
    UIImageView*headImageView;
    UIButton*cancalButton;
    UIButton*sureButton;
    
    NSInteger typeIdx;
}

@end
@implementation BankSelectView

+ (instancetype)showInView:(UIView *)view{
    BankSelectView *tmp = [[BankSelectView alloc] initWithFrame:view.bounds];
    [view addSubview:tmp];
    [tmp show];
    return tmp;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadViews];
    }
    return self;
}

- (void)loadViews{
    self.alpha = 0;
    self.backgroundColor = [UIColor colorWithWhite:1 alpha:0.6];
    contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, 350)];
    [self addSubview:contentView];
    
    //add btn to contentView...
    headImageView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 10, self.frame.size.width, 40)];
    headImageView.image=[UIImage imageNamed:@"bg_tf.png"];
    [contentView addSubview:headImageView];
    cancalButton=[[UIButton alloc]initWithFrame:CGRectMake(10, 15, 80, 30)];
    [cancalButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancalButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [cancalButton addTarget:self action:@selector(cancelButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:cancalButton];
    sureButton=[[UIButton alloc]initWithFrame:CGRectMake(240, 15, 80, 30)];
    [sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [sureButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [sureButton addTarget:self action:@selector(SureButtonClick) forControlEvents:UIControlEventTouchUpInside];

    [contentView addSubview:sureButton];
    
    bankPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, self.frame.size.width, 300)];
    bankPicker.delegate = self;
    bankPicker.dataSource = self;
    [contentView addSubview:bankPicker];
    bankArray = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"BankAndType.plist" ofType:nil]];//银行卡
    
    //初始化默认数据
    self.selBank=[[SelectBank alloc] init];
    _selBank.bankType = bankArray[0][@"name"];
    _selBank.bankName = bankArray[0][@"bank"][0][@"name"];

}
-(void)cancelButtonClick{
 [self hide];

}
-(void)SureButtonClick{
    
    [self.target getBankInfo:self.selBank.bankName bankType:self.selBank.bankType];
//    [self getBankInfo];
}
//-(void)getBankInfo{
//    NSLog(@"111111");
//}
- (void)show{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:10 options:0 animations:^{
        self.alpha = 1;
        contentView.transform = CGAffineTransformMakeTranslation(0, -bankPicker.frame.size.height);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide{
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.7 initialSpringVelocity:10 options:0 animations:^{
        self.alpha = 0;
        contentView.transform = CGAffineTransformMakeTranslation(0, 0);
    } completion:^(BOOL finished) {
        
    }];
}

- (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent *)event{
//    [self hide];
}

#pragma mark - picker
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return bankArray.count;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
//    return 2;
    if (component == 0) {
        NSArray *tmp = bankArray[component][@"bank"];
        return [tmp count];
    }else if (component == 1){
        return [bankArray count];
    }
    return 0;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return @"1";
    if (component == 0) {
        NSArray *tmp = bankArray[typeIdx][@"bank"];
        return tmp[row][@"name"];
    }else if (component == 1){
        return bankArray[row][@"name"];
    }
    return @"";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 1) {
        typeIdx = row;
        _selBank.bankType = bankArray[row][@"name"];
        _selBank.bankName = bankArray[row][@"bank"][0][@"name"];
        [pickerView selectRow:0 inComponent:0 animated:NO];
        [pickerView reloadComponent:0];
    }else if(component == 0){
        _selBank.bankName = bankArray[typeIdx][@"bank"][row][@"name"];
    }
}

@end
